package com.ebay.buyer;

import java.util.LinkedHashMap;

import com.ebay.model.*;
import com.ebay.services.Services;

import com.opensymphony.xwork2.ActionSupport;


public class DisplayItemList extends ActionSupport{
	
	public LinkedHashMap<String, String> getSubCatList() {
		return subCatList;
	}

	public void setSubCatList(LinkedHashMap<String, String> subCatList) {
		this.subCatList = subCatList;
	}

	public int getSubcategory_id() {
		return subcategory_id;
	}

	public void setSubcategory_id(int subcategory_id) {
		this.subcategory_id = subcategory_id;
	}

	public int getCategory_id() {
		return category_id;
	}

	public void setCategory_id(int category_id) {
		this.category_id = category_id;
	}

	public int getFilterBuyType() {
		return filterBuyType;
	}

	public void setFilterBuyType(int filterBuyType) {
		this.filterBuyType = filterBuyType;
	}

	public float getFilterPriceRangeLow() {
		return filterPriceRangeLow;
	}

	public void setFilterPriceRangeLow(float filterPriceRangeLow) {
		this.filterPriceRangeLow = filterPriceRangeLow;
	}

	public float getFilterPriceRangeHigh() {
		return filterPriceRangeHigh;
	}

	public void setFilterPriceRangeHigh(float filterPriceRangeHigh) {
		this.filterPriceRangeHigh = filterPriceRangeHigh;
	}

	public int getFilterItemsToDisplay() {
		return filterItemsToDisplay;
	}

	public void setFilterItemsToDisplay(int filterItemsToDisplay) {
		this.filterItemsToDisplay = filterItemsToDisplay;
	}

	public int getFilterCondition() {
		return filterCondition;
	}

	public void setFilterCondition(int filterCondition) {
		this.filterCondition = filterCondition;
	}

	public int getFilterSort() {
		return filterSort;
	}

	public void setFilterSort(int filterSort) {
		this.filterSort = filterSort;
	}

	public LinkedHashMap<String, Object> getItemList() {
		return itemList;
	}

	public void setItemList(LinkedHashMap<String, Object> itemList) {
		this.itemList = itemList;
	}

	public int getTotalPages() {
		return totalPages;
	}
	
	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int subcategory_id;
	private int category_id;
	private LinkedHashMap<String,Object> itemList = new LinkedHashMap<String, Object>();
	private LinkedHashMap<String,String> subCatList = new LinkedHashMap<String, String>();

	private int filterBuyType;
	private float filterPriceRangeLow;
	private float filterPriceRangeHigh;
	private int filterItemsToDisplay;
	private int filterCondition;
	private int filterSort;
	private int pageNumber;
	private int totalPages;
	
	public DisplayItemList(){
		super();
	}

	public String execute()
	{	
		String queryFilters = generateFilterQuery();
		switch(category_id){
		case 1:
			//Book!
			totalPages = Book.getBookList(queryFilters,itemList);
			break;
			default:
				break;
		}
		
		totalPages = totalPages/filterItemsToDisplay + ((totalPages%filterItemsToDisplay>0)?1:0);
		
		//get the subcategory list to be displayed in side.
		subCatList = Services.getSubcategoryList(category_id, subcategory_id);
		System.out.println("Pages : "+totalPages);
		return SUCCESS;
		
	}

	private String generateFilterQuery() {
		String queryFilter = "";
		if(subcategory_id!=0){
			queryFilter += " AND subcategory_id = "+subcategory_id;
		}
		
		switch(filterBuyType){
		case 1:
			queryFilter += " AND cond = 'new' ";
			break;
		case 2:
			queryFilter += " AND cond = 'old' ";
			break;
			default:
				break;
		}
		
		if(filterPriceRangeHigh!=0||filterPriceRangeLow!=0){
			if(filterPriceRangeHigh<filterPriceRangeLow){
				float temp = filterPriceRangeHigh;
				filterPriceRangeHigh = filterPriceRangeLow;
				filterPriceRangeLow = temp;
			}
			queryFilter += " AND Price > "+filterPriceRangeLow+" AND Price < "+filterPriceRangeHigh+" ";
				
		}
		
		switch (filterSort) {
		case 1:
			queryFilter += " ORDER BY Price ASC ";
			break;
		case 2:
			queryFilter += " ORDER BY Price DESC ";
			break;
		case 3:
			queryFilter += " ORDER BY expdate ASC ";
			break;
		case 4:
			queryFilter += " ORDER BY expdate DESC ";

		default:
			break;
		}
		
		filterItemsToDisplay=(filterItemsToDisplay==0)?1:filterItemsToDisplay;
		queryFilter += " LIMIT "+pageNumber*filterItemsToDisplay+","+filterItemsToDisplay;
			
		return queryFilter;
	}
}
